package servlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Notice;
import DAO.NoticeDao;

public class NoticeModifyAction implements Action {

	@Override
	public ActionForward execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Notice notice = new Notice();
		NoticeDao noticeDao = new NoticeDao();
		
		HttpSession session=request.getSession();

		ActionForward forward=new ActionForward();
		
			
		int studyId = request.getParameter("studyId") != null
				&& request.getParameter("studyId") != "" ? Integer
				.parseInt(request.getParameter("studyId")) : 0;
		int userId = session.getAttribute("userid") != null
				&& session.getAttribute("userid") != "" ? Integer
				.parseInt((String)session.getAttribute("userid")) : 0;
		int noticeId = request.getParameter("noticeId") != null
				&& request.getParameter("noticeId") != "" ? Integer
					.parseInt(request.getParameter("noticeId")) : 0;
					
		if(request.getParameter("noticeId")==null)			
		{
		System.out.println("noticeID: "+ noticeId );
		}
				
		if (userId < 1) {
			session.invalidate();
			Util.ShowError(response, "로그인해주세요");
		}
		if (studyId < 1 || noticeId < 1) {
			Util.ShowError(response, "잘못 된 접근입니다");
		}
		
		request.setAttribute("studyId", studyId);	
		
		String title = request.getParameter("title")!= null && request.getParameter("title") 
				!= ""? request.getParameter("title") : null;	
		String content = request.getParameter("content")!= null && request.getParameter("content") 
				!= ""? request.getParameter("content") : null;		
			

				
			
		if(content==null || title==null)  {
			//error return
			Util.ShowError(response, "값을 올바로 입력해주세요");
			
			return null;
		}
		title=Util.toUTF8(title);
		content=Util.toUTF8(content);

		notice.setSid(studyId);
		notice.setUid(userId);
		notice.setNoticeId(noticeId);
		notice.setTitle(title);
		notice.setContent(content);
	
		
		noticeDao.updateNoticeInfo(notice);
	
		
	
		// go to details studyId;
		

		forward.setPath("./study_notice.jsp");
		return forward;
	
	}

}
